package utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * That class is responsible for generates a digest, i.e. a resume of the message to be possible to compare
 * the message integrity in the receiver side.
 */
public class TextMessageDigest {

    /**
     * Given a string with a message this method generates a digest to it using the md5 hash algorithm.
     * 
     * @param message the message to be digested.
     * 
     * @return the string generated by the digestion of the message given.
     */
    public static String digest(Object message) {     
    	
        byte[] plainText = Serialization.Serialize(message);        
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(plainText);
	        byte[] digested = messageDigest.digest();
	        return Base64Processing.encodeData(digested);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}        
		return null;
    }
}